package sqls

import (
	"database/sql"
	"fmt"
	"gitee.com/youbeiwuhuan/go-xxljob/tools/reflects"
	"reflect"
)

// ToNamedArgs 结构体对象转 参数列表
func ToNamedArgs(v interface{}) []sql.NamedArg {
	if v == nil {
		return nil
	}

	if !reflects.IsStruct(v) {
		return nil
	}

	valVo := reflect.ValueOf(v)
	if 0 == valVo.NumField() {
		return nil
	}

	typeVo := valVo.Type()

	nameParams := make([]sql.NamedArg, typeVo.NumField(), typeVo.NumField())

	for i := 0; i < valVo.NumField(); i++ {
		fmt.Println(fmt.Sprintf("----------%s    【%+v】  %+v %+v",
			typeVo.Field(i).Name, valVo.Field(i).Interface(), reflects.IsNil(valVo.Field(i).Interface()), reflects.IsZeroValue(valVo.Field(i).Interface())))

		if reflects.IsZeroValue(valVo.Field(i).Interface()) {
			continue
		}

		nameParams = append(nameParams, sql.Named(typeVo.Field(i).Name, valVo.Field(i).Interface()))

	}
	return nameParams
}
